package com.jiaai.cloud.wx.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiaai.cloud.base.entity.RestResult;
import com.jiaai.cloud.base.entity.RestResultGenerator;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.page.CustomPage;
import com.jiaai.cloud.base.page.FrontPage;
import com.jiaai.cloud.base.util.GeoHash;
import com.jiaai.cloud.wx.entity.*;
import com.jiaai.cloud.wx.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-22
 */
@Controller
@RequestMapping("/api/v1/twxshop")
public class TWxshopController {
    @Autowired
    private ITWxshopService itWxshopService;
    @Autowired
    private ITWxcommentService itWxcommentService;
    @Autowired
    private ITWxgoodsService itWxgoodsService;
    @Autowired
    private ITWxgoodsclassService itWxgoodsclassService;
    @Autowired
    private ITWxalterimgService itWxalterimgService;
    @Autowired
    private ITLifecycleService itLifecycleService;

    //todo tested
    /**
     * 基本商店查询
     * @param shopid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/findById/{shopid}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object findById(@PathVariable("shopid") Integer shopid)  throws ServiceException {

        try {
            //返回的商店shop是带有商品goods的list
            TWxshop shop = itWxshopService.selectShopGoods(shopid);

            TWxshop tWxshop = new TWxshop();
            tWxshop.setShopid(shopid);
            tWxshop.setLookedtimes((shop.getLookedtimes()+1));
            itWxshopService.updateById(tWxshop);

            RestResult result = RestResultGenerator.genSuccessResult(shop);
            return result;
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //todo
    /**
     * 查询商店的所有评论
     * @param shopid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/queryshopcomment/{shopid}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object queryShopComment(@PathVariable("shopid") Integer shopid) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itWxcommentService.selectShopCom(shopid));
        } catch (Exception e){
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 用户创建商店信息
     * @param tWxshop

     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/reg",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object reg(@RequestHeader("userid") Integer userid
            , @RequestBody TWxshop tWxshop)  throws ServiceException {
        try {
            tWxshop.setRegdate(new Date());
            tWxshop.setGeohash(new GeoHash(Double.parseDouble(tWxshop.getLat()), Double.parseDouble(tWxshop.getLng())).getGeoHashBase32());
            itWxshopService.insert(tWxshop);
            itWxshopService.insertUserShop(userid, tWxshop.getShopid());
            return RestResultGenerator.genSuccessResult("用户创建商店成功");
        }catch (Exception e) {
            throw new ServiceException("创建失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 删除商店信息, 并删除用户商店中间表的记录, 商品, 商品分类
     * @param shopid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/delete/{shopid}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    @Transactional
    public Object delete(@PathVariable("shopid") Integer shopid)  throws ServiceException {
        try {
            itWxshopService.deleteUserShop(shopid);
            itWxgoodsclassService.delete(new EntityWrapper<TWxgoodsclass>().eq("shopid", shopid));
            List<Integer> ids = itWxshopService.selectGoodsids(shopid);
            itWxshopService.deleteShopGoodss(shopid);
            for (Integer id : ids){
                itWxgoodsService.deleteById(id);
            }
            itWxcommentService.delete(new EntityWrapper<TWxcomment>().eq("shopid", shopid));
            itWxalterimgService.delete(new EntityWrapper<TWxalterimg>().eq("imgshopid", shopid));
            itWxshopService.deleteById(shopid);

            EntityWrapper<TLifecycle> ew = new EntityWrapper<TLifecycle>();
            ew.eq("shopid", shopid);
            itLifecycleService.delete(ew);

            return RestResultGenerator.genSuccessResult("删除商店成功");
        }catch (Exception e) {
            throw new ServiceException("删除失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 修改商店信息
     * @param shopid
     * @param tWxshop
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/update/{shopid}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object update(@PathVariable("shopid") Integer shopid, @RequestBody TWxshop tWxshop)  throws ServiceException {
        try {
            tWxshop.setShopid(shopid);
            itWxshopService.updateById(tWxshop);
            return RestResultGenerator.genSuccessResult("修改商店成功");
        }catch (Exception e) {
            throw new ServiceException("修改失败", e.getMessage());
        }
    }


    //todo
    /**
     * 根据商店分类id, 查询商店
     * @param frontPage
     * @param sclassid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getshopbyclass/{sclassid}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object getShopByClass(FrontPage frontPage, @PathVariable("sclassid") Integer sclassid)  throws ServiceException {
        Page page = frontPage.getPagePlus();
        page.setOpenSort(false);
        page.setSearchCount(false);
        page.setSize(10);

        EntityWrapper<TWxshop> ew = new EntityWrapper<TWxshop>();
        ew.eq("sclassid", sclassid).setSqlSelect("shopid, shopname, shopavater, introduce, regdate, address, phonenumber");
        ew.orderBy("lookedtimes", false);
        try {
            return RestResultGenerator.genSuccessResult(new CustomPage(itWxshopService.selectPage(page, ew)));
        }catch (Exception e) {
            throw new ServiceException("获取失败", e.getMessage());
        }
    }
}
